//
//  SearchController.m
//  i5db
//
//  Created by iMuse on 15-5-27.
//  Copyright (c) 2015年 mySelf. All rights reserved.
//

#import "SearchController.h"
#import "AppCenter.h"
@interface SearchController ()
{
    UIView * searchView;
    UIView * contentView;
    UIButton * cancelButton;
    
    UIImageView * searchImage;
    UITextField * searchField;
    
}

@end

@implementation SearchController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
-(void)updateViewConstraints
{
    [super updateViewConstraints];
    [searchView autoPinToTopLayoutGuideOfViewController:self withInset:0];
    [searchView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [searchView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    [searchView autoSetDimension:ALDimensionHeight toSize:40];
    
    [contentView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(5, 5, 5, 40)];
    [cancelButton autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:contentView withOffset:2];
    [cancelButton autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:5];
    [cancelButton autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:5];
    [searchImage autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:5];
    [searchImage autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:5];
    [searchImage autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:5];
    [searchImage autoSetDimension:ALDimensionWidth toSize:20];
    
    [searchField autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:searchImage withOffset:2];
    [searchField autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:0];
    [searchField autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:0];
    [searchField autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setNeedsUpdateConstraints];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initView];
 
}
-(void) initView
{
    searchView = [UIView newAutoLayoutView];
    searchView.alpha = 0.8;
    searchView.backgroundColor = [AppCenter color:@"235,235,241,1"];
    [self.view addSubview:searchView];
    
    contentView = [UIView newAutoLayoutView];
    contentView.layer.cornerRadius = 4;
    contentView.backgroundColor = [UIColor whiteColor];
    [searchView addSubview:contentView];
    cancelButton = [UIButton newAutoLayoutView];
    [cancelButton addTarget:self action:@selector(cancelButton:) forControlEvents:UIControlEventTouchUpInside];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [cancelButton setTitleColor:[AppCenter color:@"76,187,248,1"] forState:UIControlStateNormal];
    [searchView addSubview:cancelButton];
    
    searchImage = [UIImageView newAutoLayoutView];
    searchImage.image =[UIImage imageNamed:@"qq_active_search_finder_grey.png"];
    [contentView addSubview:searchImage];
    
    searchField = [UITextField newAutoLayoutView];
    [searchField becomeFirstResponder];
    searchField.clearButtonMode = UITextFieldViewModeAlways;
    searchField.placeholder = @"聚会、合作";
    searchField.font = [UIFont systemFontOfSize:12];
    [contentView addSubview:searchField];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textDidChanged:) name:UITextFieldTextDidChangeNotification object:searchField];
    
   
    
  
}
-(void)textDidChanged:(NSNotification*) notification
{
    UITextField * field = (UITextField*) notification.object;
    UITextRange * range =  [field markedTextRange];
    if (range==nil || range.empty)
    {
        
       // NSString * string = [AppCenter string:field.text];
        
        //请求服务器
    }

}
-(void)cancelButton:(UIButton*) button
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewWillAppear:(BOOL)animated {

   
    [super viewWillAppear:animated];
    
    [[self rdv_tabBarController] setTabBarHidden:YES animated:NO];
   
    
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [[self rdv_tabBarController] setTabBarHidden:NO animated:NO];
    
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
   
}



@end
